<template>
    <el-container>
        <el-header style="border-bottom: none;height: 50px;">
            <div class="left-panel">
                <div class="finish">
                    <div class="finishNum">合计金额:
                        <div class="text">{{ balance_total }}</div>
                    </div>
                </div>
            </div>
        </el-header>
        <el-header>
            <div class="left-panel">
                <el-button type="primary" icon="el-icon-plus" v-auth="'account.add'" @click="add"></el-button>
                <el-button type="danger" plain icon="el-icon-delete" v-auth="'account.delete'" :disabled="selection.length == 0" @click="batch_del"></el-button>
                <el-button v-auth="'account.add'" @click="cate_add">付款方式管理</el-button>
                <sc-import-button :auth="'account.import'" :apiObj="importObj" :base="base + '/file_templets/temples_send.xlsx'" @importSuccess="importSuccess" ></sc-import-button>
                <sc-export-button :auth="'account.export'" :apiObj="apiObj" :search="search" :selection="selection"></sc-export-button>
            </div>
            <div class="right-panel">
                <div class="right-panel-search">
                    <el-input v-model="search.account" placeholder="账号" clearable @keyup.enter="upsearch"></el-input>
                    <el-input v-model="search.account_name" placeholder="开户行" clearable @keyup.enter="upsearch"></el-input>
                    <el-select v-model="search.system_disable" placeholder="使用状态" clearable>
                        <el-option label="开始" :value="0"></el-option>
                        <el-option label="停用" :value="1"></el-option>
                    </el-select>
                    <el-button type="primary" icon="el-icon-search" @click="upsearch"></el-button>
                    <el-button type="primary" icon="el-icon-refresh" @click="refresh"></el-button>
                </div>
            </div>
        </el-header>
        <el-main class="nopadding">
            <scTable ref="table" row-key="id" :apiObj="apiObj" @cell-click="cellClick" @selection-change="selectionChange" stripe remoteSort remoteFilter border @setExtend="setExtend">
                <el-table-column type="selection" width="50" reserve-selection align="center"></el-table-column>
                <el-table-column type="index" label="" fixed align="center" width="50"></el-table-column>
                <el-table-column label="账号" prop="account" align="center"></el-table-column>
                <el-table-column label="户名" prop="account_name" align="center"></el-table-column>
                <el-table-column label="开户行" prop="account_bank" align="center"></el-table-column>
                <el-table-column label="币种" prop="currency.name" align="center"></el-table-column>
                <el-table-column label="金额" prop="balance" align="center"></el-table-column>
                <el-table-column label="付款方式" prop="paytype.paytype_name" align="center"></el-table-column>
                <el-table-column label="创建人" prop="creator_name" align="center"></el-table-column>
                <el-table-column label="创建时间" prop="create_time" align="center"></el-table-column>
                <el-table-column label="状态" prop="system_disable" width="80" align="center">
                    <template #default="scope">
                        <el-switch v-model="scope.row.system_disable" @change="changeSwitch($event, scope.row)" :loading="scope.row.$switch_status" :active-value="0" inline-prompt active-text="启用" :inactive-value="1" inactive-text="禁用"></el-switch>
                    </template>
                </el-table-column>
                <el-table-column label="操作" fixed="right" align="center" width="270">
                    <template #default="scope">
                        <div class="buttonFlex">
                            <el-button type="primary" size="small" @click="table_show(scope.row, scope.$index)" v-auth="'account.show'">查看</el-button>
                            <el-button type="primary" size="small" @click="table_edit(scope.row, scope.$index)" v-auth="'account.edit'">编辑</el-button>
                            <el-popconfirm title="确定删除吗？" @confirm="table_del(scope.row, scope.$index)">
                                <template #reference>
                                    <el-button type="danger" size="small" v-auth="'account.delete'">删除</el-button>
                                </template>
                            </el-popconfirm>
                            <el-button size="small" @click="table_record(scope.row, scope.$index)" v-auth="'account.record'">流水</el-button>
                        </div>
                    </template>
                </el-table-column>
            </scTable>
        </el-main>
    </el-container>
	<save-dialog v-if="dialog.save" ref="saveDialog" @success="handleSuccess" @closed="dialog.save = false"></save-dialog>
    <pay-cate-list v-if="dialog.catelist" ref="catelistDialog" @success="handleSuccess" @closed="dialog.catelist = false"></pay-cate-list>
    <record-list v-if="dialog.record" ref="recordDialog" @success="handleSuccess" @closed="dialog.record = false" :query="{account_id: account_id}"></record-list>
</template>

<script>
import PayCateList from './payCateList.vue'
import saveDialog from './save'
import config from '@/config'
import RecordList from '@/views/business/finance/record/recordList.vue'

export default {
	name: '需方列表',
	components: {
		saveDialog, PayCateList, RecordList,
	},
	data() {
		return {
			dialog: {
				save: false,
				catelist: false,
				record: false,
			},
			apiObj: this.$API.basic.account.list,
            base: config.API_URL,
			importObj: this.$API.basic.account.import,
			selection: [],
			search: {
				keyword: null,
			},
            balance_total: 0,
            account_id: "",
		}
	},
	mounted() {
	},
	methods: {
        setExtend(data){
            this.balance_total = data.balance_total
        },
        // 导入成功
        importSuccess(){
            this.$refs.table.refresh()
        },
		//添加
		add() {
            this.dialog.save = true
			this.$nextTick(() => {
				this.$refs.saveDialog.open('add')
			})
		},
		//编辑
		table_edit(row) {
			this.dialog.save = true
			this.$nextTick(() => {
				this.$refs.saveDialog.open('edit').setData(row)
			})
		},
		//查看
		table_show(row) {
			this.dialog.save = true
			this.$nextTick(() => {
				this.$refs.saveDialog.open('show').setData(row)
			})
		},
		//表格内开关
		async changeSwitch(val, row) {
			row.$switch_status = true;
			var res = await this.$API.basic.account.update_status.post({
				id: row.id,
				system_disable: row.system_disable,
			});
			if (res.code == 1) {
				row.$switch_status = false;
				this.$refs.table.refresh();
				this.$message.success("操作成功");
			} else {
				this.$alert(res.message, "提示", {type: "error"});
			}
		},
		//删除
		async table_del(row) {
			var reqData = {id: row.id}
			var res = await this.$API.basic.account.delete.post(reqData);
			if (res.code == 1) {
				this.$refs.table.refresh()
				this.$message.success("删除成功")
			} else {
				this.$alert(res.message, "提示", {type: 'error'})
			}
		},
		async table_del_all(delete_ids) {
			var reqData = {id: delete_ids}
			var res = await this.$API.basic.account.delete.post(reqData);
			if (res.code == 1) {
				this.$refs.table.refresh()
				this.$message.success("删除成功")
			} else {
				this.$alert(res.message, "提示", {type: 'error'})
			}
		},
		//批量删除
		async batch_del() {
			var delete_ids = [];
			this.selection.forEach((item) => {
				delete_ids.push(item.id)
			})
			this.$confirm(`确定删除选中的 ${this.selection.length} 项吗？`, '提示', {
				type: 'warning'
			}).then(() => {
				this.table_del_all(delete_ids)
			}).catch(() => {

			})
		},
		//表格选择后回调事件
		selectionChange(selection) {
			this.selection = selection;
		},
		//搜索
		upsearch() {
			this.$refs.table.upData(this.search)
		},
		refresh() {
			for (const key in this.search) {
				this.search[key] = null;
			}
			this.$refs.table.upData(this.search)
		},
		//本地更新数据
		handleSuccess(data, mode) {
			if (mode == 'add') {
				this.$refs.table.refresh()
			} else if (mode == 'edit') {
				this.$refs.table.refresh()
			}
		},

        cate_add(){
            this.dialog.catelist = true
            this.$nextTick(() => {
                this.$refs.catelistDialog.open('add')
            })
        },

        // 流水记录
        table_record(row){
            this.account_id = row.id
            this.dialog.record = true
            this.$nextTick(() => {
                this.$refs.recordDialog.open('add')
            })
        }
	}
}
</script>

<style></style>
